﻿using System;
using System.Linq;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Client;
using T.FAS.Runtime.Metadata.SPI;
using T.FAS.Runtime.StateMachine.Service;
using T.FAS.Runtime.DataAccess.SPI;
using T.FAS.Runtime.DataAccess.Service;
using System.Collections.Generic;
using T.FAS.Runtime.DataAccess.SGSPI;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.DataAccess.SGPlugin
{

    [SGService]
    public class StateMachineRTPService : IStateMachineRTPService
    {
        StateMachineRTPClient StateMachineService = StateMachineRTPClient.GetInstance();

        /// <summary>
        /// CDO给业务流提供的方法
        /// </summary>
        /// <param name="CDOID">CDO元数据id</param>
        /// <param name="ColKey">绑定状态机的列名</param>
        /// <param name="EventKey">状态机事件Key</param>
        /// <param name="record">record数据</param>
        /// <returns></returns>
        [SGService(
            ServiceID = "FASR-ChangeState",
            Describtion = "状态机ChangeState事件，触发状态变换的事件以及动作",
            ServiceType = ServiceType.Retrieve)]
        public TResult ChangeState(string CDOID, string ColKey, string EventKey, TRecord record)
        {
            return StateMachineService.ChangeState(CDOID, ColKey, EventKey, record);
        }

        /// <summary>
        /// CDO给审批流提供的方法，前置条件为BillID单据已存在，只需要执行Update方法
        /// </summary>
        /// <param name="CDOID">CDO元数据id</param>
        /// <param name="ColKey">绑定状态机的列名</param>
        /// <param name="EventKey">状态机事件Key</param>
        /// <param name="BillId">单据id</param>
        /// <param name="Source">来源</param>
        /// <returns></returns>
        public TResult FireEvent(string CDOID, string ColKey, string EventKey, string BillId, string Source = null)
        {
            return StateMachineService.FireEvent(CDOID, ColKey, EventKey, BillId, Source);
        }

        /// <summary>
        /// CDO给审批流提供的方法，前置条件为BillID单据已存在，只需要执行Update方法
        /// </summary>
        /// <param name="cdoId">CDO元数据id</param>
        /// <param name="colKey">绑定状态机的列名</param>
        /// <param name="eventKey">状态机事件Key</param>
        /// <param name="record">单据id</param>
        /// <param name="source">来源</param>
        /// <returns></returns>
        public TResult FireEventWithTRecord(string cdoId, string colKey, string eventKey, TRecord record, string source = null)
        {
            return StateMachineService.FireEventWithTRecord(cdoId, colKey, eventKey, record, source);
        }

        /// <summary>
        /// 运行时获取下个阶段可访问的状态集合，需要对接权限系统
        /// </summary>
        /// <param name="StateMachineID">状态机id</param>
        /// <param name="stateKey">状态key（int类型）</param>
        /// <returns></returns>
        public List<TState> GetTransitionStates(string StateMachineID, int stateKey, TRecord record = null)
        {
            return StateMachineService.GetTransitionStates(StateMachineID, stateKey, record);
        }

    }
}
